/**
 * User interface elements
 */
var UI = {
  
   /**
    * Unselect/Select line in table after 
    * press checkbox
    * @param element - element name
    * @param _class - css class    
    */  
    selectLine: function(element, _class)
    {
        $(element).on("click",function(){
            var p = $(this).parent().parent();
            if($(this).is(':checked'))
            {             
                p.find('td').each(function(){
                    $(this).addClass(_class); 
                });
            }else
            {
                p.find('td').each(function(){
                    $(this).removeClass(_class); 
                });
            }                      
        });
    },
    
    /**
     * Toggle button
     */
    toggle: function(element)
    {        
        var toggle = $(element).attr('toggle');
            
        if(toggle)
        {

            var id = $(element).attr('id');
            if(toggle == "collapsed")
            {
                
                $(element).attr('toggle','expanded').attr('src','/local/toggle_expand.png');
                $('div[id="'+id+'"]').slideUp(1500, function(){
                $(this).css('display','none');
               });
            }else 
            {                
                $(element).attr('toggle','collapsed').attr('src','/local/toggle_collapse.png');                
               $('div[id="'+id+'"]').slideDown(1500, function(){
                $(this).css('display','block');
               });
            }
        }
    },
    
    /**
     * Scrollbar
     * Initalize and set scrollbar events to divs. Div parent must have 
     * scroll atrib with scrolled div id value.
     */
    scrollbar: function()
    {        
        var wnd =  $('.scrollbar'); 
        var ctrl = $('.scrollbar-control');               
        var cnt = wnd.find('#'+wnd.attr('rev'));
        wnd.after('<div class="scrollbar-control">&nbsp;</div>');
        wnd.width(cnt.parent().width());
        
        var mSpace = cnt.height()-wnd.height();
        
        if(mSpace < 0 ){    
            $('div[class="scrollbar-control"]').remove();
            return false;
        }
            
        var ctrlH = wnd.height() - mSpace ;
        var ratio=1;

        if(ctrlH <= 40)
        {				
            ctrlH = 40;
            ratio =  (mSpace) / (wnd.height()-40) ;
        }
        ctrl.height(ctrlH);
        
        var offset = 0;
        
        ctrl.mousedown(function(event){
            ctrl.addClass('scroll');
            offset  = event.pageY - wnd.offset().top;
            
            event.preventDefault();
        });
        
        $(document).mouseup(function(event){
            if(ctrl.hasClass('scroll'))
            {
                ctrl.removeClass('scroll');
            }
        }).mousemove(function(event){
            if(ctrl.hasClass('scroll'))
            {
                oy = (event.pageY - wnd.offset().top) - offset;						
                if(oy >= 0 && oy+ctrl.height() <= wnd.height()){					
                    ctrl.css('top',oy);
                    oy=oy*ratio;
                    cnt.css('margin-top',-oy);
                }
                
            }
        });          
    }
};